package org.academic.texteditor.gui.component.menu;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPopupMenu;

public class ShowPopupListener implements ActionListener {

	public static final int HORIZONTAL = 1;
	public static final int VERTICAL = 2;
	
	private final JPopupMenu popup;
	private final int position;

	public ShowPopupListener(JPopupMenu popup) {
		this(popup, VERTICAL);
	}
	
	public ShowPopupListener(JPopupMenu popup, int position) {
		this.popup = popup;
		this.position = position;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Component component = (Component) e.getSource();

		Point p = component.getLocationOnScreen();
		int x = p.x;
		int y = p.y;
		
		if (position == HORIZONTAL) {
			x += component.getWidth();
		} else if (position == VERTICAL) {
			y += component.getHeight();
		}
		
		popup.setLocation(x, y);
		popup.setVisible(true);
	}

}
